<template>
  <div class="users">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
      <el-breadcrumb-item>活动详情</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索框 -->
    <div class="users_div">
      <h3>用户列表</h3>
      <el-input
        placeholder="请输入内容"
        v-model="input"
        class="input-with-select"
      >
        <el-button
          slot="append"
          @click="search()"
          icon="el-icon-search"
        ></el-button>
      </el-input>
    </div>
    <el-card class="cards" shadow="always">
      <el-button type="primary" @click="add()">添加</el-button>
    </el-card>
    <!-- 数据表格 -->
    <el-table
      ref="multipleTable"
      :data="userList.users"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="username" label="姓名" width="120">
      </el-table-column>
      <el-table-column prop="mobile" label="手机号" width="180">
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="180"> </el-table-column>
      <el-table-column prop="create_time" label="创建时间" width="180">
      </el-table-column>
      <el-table-column prop="role_name" label="角色姓名" width="180">
      </el-table-column>
      <el-table-column label="操作" width="250">
        <el-button type="primary">编辑</el-button>
        <el-button type="danger">删除</el-button>
      </el-table-column>
    </el-table>
    <!-- 对话框 --> 
    <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
      <!-- 表单 -->
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            type="text"
            v-model="ruleForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input
            type="text"
            v-model="ruleForm.phone"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input
            type="email"
            v-model="ruleForm.email"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即添加</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "Users",
  data() {
    //   手机验证
    var validatePhone = (rule, value, callback) => {
      if (!/^1[3|4|5|7|8][0-9]{9}$/.test(value)) {
        callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    };
    //   邮箱验证
    var validateEmail = (rule, value, callback) => {
      if (
        !/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(
          value
        )
      ) {
        callback(new Error("邮箱格式错误"));
      } else {
        callback();
      }
    };

    return {
      input: "",
      userList: {},
      dialogVisible: false,
      ruleForm: {
        username: "",
        password: "",
        email: "",
        phone: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
          phone: [
            { validator: validatePhone, trigger: 'blur' }],
        email: [
           { validator: validateEmail, trigger: 'blur' }]
        
      },
    };
  },
  methods: {
    search() {
      console.log();
    },
    // 获取用户列表
    async getUsersList() {
      var p1 = await this.axios.get("users?pagenum=1&pagesize=10");
      this.userList = p1.data.data;
    },
    // 添加用户信息
    add() {
      this.dialogVisible = true;
    },
    submitForm(formName) {
      this.$refs[formName].validate( async (valid) => {
        // 
        if (valid) {
          var str=`username=${this.ruleForm.username}&password=${this.ruleForm.password}&email=${this.ruleForm.email}&mobile=${this.ruleForm.phone}`;
          var p1=await this.axios.post("users",str);
          this.dialogVisible=false;
          this.getUsersList();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    handleSelectionChange() {},
  },
  mounted() {
    this.getUsersList();
  },
};
</script>
<style lang="scss">
.users {
  text-align: left;
  .users_div {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .input-with-select {
      width: 400px;
    }
  }
}
</style>